package com.itheima.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.CheckItemDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckitemServiceImpl implements CheckitemService {

    @Autowired
    private CheckItemDao checkitemDao;

    //增加
    @Override
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);

    }

    //分页查询
    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<CheckItem> page = checkitemDao.findPage(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //删除
    @Override
    public void deleteById(Integer id) throws RuntimeException {
        long count = checkitemDao.findCountByCheckItemId(id);
        if (count > 0) {
           throw new RuntimeException("检查项被引用,不能删除");
        }
        checkitemDao.deleteById(id);
    }

    //编辑回显数据
    @Override
    public CheckItem findById(Integer id) {
       return checkitemDao.findById(id);
    }

    //编辑
    @Override
    public void edit(CheckItem checkItem) {
        checkitemDao.edit(checkItem);
    }

    @Override
    public List<CheckItem> findAll() {
        return checkitemDao.findAll();
    }
}
